/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.strategy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.customize.mysql.utils.MysqlUtil;
import com.pig4cloud.pigx.strategy.dto.StrategyPositionContractConditionDTO;
import com.pig4cloud.pigx.strategy.entity.StrategyPositionContract;
import com.pig4cloud.pigx.strategy.enums.StrategyPositionContractEnum;
import com.pig4cloud.pigx.strategy.mapper.StrategyPositionContractMapper;
import com.pig4cloud.pigx.strategy.mapstruct.StrategyPositionContractStruct;
import com.pig4cloud.pigx.strategy.service.StrategyPositionContractService;
import com.pig4cloud.pigx.strategy.vo.StrategyPositionContractVO;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 策略仓位-合约
 *
 * @since 2022-11-24 10:39:27
 */
@Service
@RequiredArgsConstructor
public class StrategyPositionContractServiceImpl extends ServiceImpl<StrategyPositionContractMapper, StrategyPositionContract> implements StrategyPositionContractService {

    /**
     * 分页查询
     *
     * @param page      分页数据
     * @param condition 查询条件
     * @return {@link Page< StrategyPositionContractVO>}
     * @since 2022/11/27 17:19
     */
    @Override
    public Page<StrategyPositionContractVO> page(Page<StrategyPositionContract> page, StrategyPositionContractConditionDTO condition) {
        QueryWrapper<StrategyPositionContract> query = new QueryWrapper<>();
        query.lambda()
                .eq(!Objects.isNull(condition.getPlatformId()), StrategyPositionContract::getPlatformId, condition.getPlatformId())
                .eq(!Objects.isNull(condition.getUserId()), StrategyPositionContract::getUserId, condition.getUserId())
                .eq(Strings.isNotBlank(condition.getSide()), StrategyPositionContract::getSide, condition.getSide())
                .eq(!Objects.isNull(condition.getTaskId()), StrategyPositionContract::getTaskId, condition.getTaskId())
                .ne(!Objects.isNull(condition.getExcludeTaskId()), StrategyPositionContract::getTaskId, condition.getExcludeTaskId())
                .eq(Strings.isNotBlank(condition.getStatus()), StrategyPositionContract::getStatus, condition.getStatus())
                .orderByDesc(StrategyPositionContract::getCreateTime);
        page = baseMapper.selectPage(page, query);
        List<StrategyPositionContract> list = page.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            return MysqlUtil.EMPTY_PAGE;
        }

        List<StrategyPositionContractVO> voList = StrategyPositionContractStruct.INSTANCE.list2VOList(list);

        // 遍历
        voList.forEach(vo -> {
            // 设置字典项
            vo.setSide(StrategyPositionContractEnum.Side.findName(vo.getSide()));
            vo.setStatus(StrategyPositionContractEnum.Status.findName(vo.getStatus()));
        });

        return MysqlUtil.transferPage(page, voList);
    }
}
